package com.easondongh.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easondongh.system.constants.Constants;
import com.easondongh.system.domain.SysDept;
import com.easondongh.system.vo.MenuTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.system.mapper.SysMenuMapper;
import com.easondongh.system.domain.SysMenu;
import com.easondongh.system.service.SysMenuService;
import org.springframework.util.CollectionUtils;

/**
 * @author EasonDongH
 * @date 2020/4/28 13:14
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService{
    
    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public List<MenuTreeNode> queryAllMenuTreeNode() {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        // 查询启用的菜单项、按order_num正序排序
        queryWrapper.eq(SysMenu.COL_ENABLE, Constants.ENABLE).orderByAsc(SysMenu.COL_ORDER_NUM);
        List<SysMenu> sysMenus = this.menuMapper.selectList(queryWrapper);
        List<MenuTreeNode> treeNodes = MenuTreeNode.MenuTreeNodeBuilder.build(Constants.MENU_TOP_LEVEL, sysMenus);
        return treeNodes;
    }

    @Override
    public List<MenuTreeNode> queryMenuTreeNodeByUserId(Integer id) {
        List<SysMenu> sysMenus = this.menuMapper.queryMenuByUid(id);
        List<MenuTreeNode> treeNodes = MenuTreeNode.MenuTreeNodeBuilder.build(Constants.MENU_TOP_LEVEL, sysMenus);
        return treeNodes;
    }

    @Override
    public List<SysMenu> loadMenuList() {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc(SysMenu.COL_ORDER_NUM);
        List<SysMenu> menus = this.menuMapper.selectList(queryWrapper);
        return menus;
    }

    @Override
    public Integer getMaxOrderNum() {
        Integer orderNum = this.menuMapper.getMaxOrderNum();
        return orderNum == null? 0: orderNum;
    }

    @Override
    public List<SysMenu> getMenuExcludeById(Integer id) {
        List<SysMenu> menus = this.loadMenuList();
        if(!CollectionUtils.isEmpty(menus)) {
            Queue<Integer> q = new LinkedList();
            q.add(id);
            while (!q.isEmpty()) {
                Integer cur = q.remove();
                // 获取cur下的所有子节点
                List<SysMenu> chids = menus.stream().filter(t -> t.getPid().equals(cur)).collect(Collectors.toList());
                // 所有子节点加入队列
                chids.forEach(c -> q.add(c.getId()));
                // 从列表中移除cur节点
                menus.removeIf(d -> d.getId().equals(cur));
            }
        }
        return menus;
    }

    @Override
    public Integer countChildMenuById(Integer id) {
        return this.menuMapper.countChildDeptById(id);
    }
}
